﻿using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace WPFMetronom
{
	[Serializable]
	class PresetObj
	{
		#region Declarations

		[NonSerialized]
		private MainWindow moMainWindow;

		private bool mbSimpleOrExtend = false;

		private bool mbIsCountDownChecked = false;
		private int miSliderCountDown = 0;

		private int miSliderSpeedSimple = 0;
		private bool mbIsSimpleTimeChecked = false;
		private int miSliderSimpleTimer = 0;

		private int miSliderSpeedExtendStart = 0;
		private int miSliderSpeedExtendFinish = 0;

		private int miSliderStepPart = 0;
		private bool mbStepOrPart = false;

		private int miSliderTimeHit = 0;
		private bool mbTimeOrHit = false;

		private int mbSliderRepeatLoop = 0;
		private bool mbIsRepeatLoopChecked = false;
		private bool mbRepeatOrLoop = false;

		private string msLanguage = "Rus";

		#endregion

		#region Constructor

		public PresetObj ( MainWindow mainWindow )
		{
			try
			{
				moMainWindow = mainWindow;
				mbSimpleOrExtend = moMainWindow.SimpleOrExtend;

				mbIsCountDownChecked = (bool) moMainWindow.checkBoxCountdown.IsChecked;
				miSliderCountDown = (int) moMainWindow.sliderCountdown.Value;

				miSliderSpeedSimple = (int) moMainWindow.sliderSpeedSimple.Value;
				mbIsSimpleTimeChecked = (bool) moMainWindow.checkBoxSimpleTime.IsChecked;
				miSliderSimpleTimer = (int) moMainWindow.sliderSimpleTimer.Value;

				miSliderSpeedExtendStart = (int) moMainWindow.sliderSpeedExtendStart.Value;
				miSliderSpeedExtendFinish = (int) moMainWindow.sliderSpeedExtendFinish.Value;

				miSliderStepPart = (int) moMainWindow.sliderStepPart.Value;
				mbStepOrPart = moMainWindow.StepOrPart;

				miSliderTimeHit = (int) moMainWindow.sliderTimeHit.Value;
				mbTimeOrHit = moMainWindow.TimeOrHit;

				mbSliderRepeatLoop = (int) moMainWindow.sliderRepeatLoop.Value;
				mbIsRepeatLoopChecked = (bool) moMainWindow.checkBoxRepeatLoop.IsChecked;
				mbRepeatOrLoop = moMainWindow.RepeatOrLoop;

				msLanguage = moMainWindow.Language;
			}
			catch ( Exception ex )
			{
				Logger.Log ( ex );
			}
		}

		#endregion

		#region Private Members

		private void ActivatePreset ( )
		{
			try
			{
				moMainWindow.SimpleOrExtend = mbSimpleOrExtend;
				if ( mbSimpleOrExtend )
				{
					moMainWindow.radioButtonSimple.IsChecked = true;
				}
				else
				{
					moMainWindow.radioButtonExtended.IsChecked = true;
				}

				moMainWindow.checkBoxCountdown.IsChecked = mbIsCountDownChecked;
				moMainWindow.sliderCountdown.Value = miSliderCountDown;

				moMainWindow.sliderSpeedSimple.Value = miSliderSpeedSimple;
				moMainWindow.checkBoxSimpleTime.IsChecked = mbIsSimpleTimeChecked;
				moMainWindow.sliderSimpleTimer.Value = miSliderSimpleTimer;

				moMainWindow.sliderSpeedExtendStart.Value = miSliderSpeedExtendStart;
				moMainWindow.sliderSpeedExtendFinish.Value = miSliderSpeedExtendFinish;

				moMainWindow.sliderStepPart.Value = miSliderStepPart;
				moMainWindow.StepOrPart = mbStepOrPart;
				moMainWindow.buttonStepPart.Content = mbStepOrPart ? "Шаг" : "Часть";

				moMainWindow.sliderTimeHit.Value = miSliderTimeHit;
				moMainWindow.TimeOrHit = mbTimeOrHit;
				moMainWindow.buttonTimeHit.Content = mbTimeOrHit ? "Время" : "Удар";

				moMainWindow.sliderRepeatLoop.Value = mbSliderRepeatLoop;
				moMainWindow.checkBoxRepeatLoop.IsChecked = mbIsRepeatLoopChecked;
				moMainWindow.RepeatOrLoop = mbRepeatOrLoop;
				moMainWindow.buttonRepeatLoop.Content = mbRepeatOrLoop ? "Повторить" : "Зациклить";

				moMainWindow.Language = msLanguage;
				// do translations ?
			}
			catch ( Exception ex )
			{
				Logger.Log ( ex );
			}
		}

		#endregion

		#region Public Methods

		public void LoadPreset ( string sFileName )
		{
			try
			{
				PresetObj poParameters;
				FileStream fsStream = new FileStream ( sFileName, FileMode.Open, FileAccess.Read, FileShare.Read );
				BinaryFormatter bfFormatter = new BinaryFormatter ( );
				poParameters = (PresetObj) bfFormatter.Deserialize ( fsStream );
				fsStream.Close ( );

				mbSimpleOrExtend = poParameters.mbSimpleOrExtend;

				mbIsCountDownChecked = poParameters.mbIsCountDownChecked;
				mbIsCountDownChecked = poParameters.mbIsCountDownChecked;

				miSliderCountDown = poParameters.miSliderCountDown;
				miSliderSpeedSimple = poParameters.miSliderSpeedSimple;
				mbIsSimpleTimeChecked = poParameters.mbIsSimpleTimeChecked;

				miSliderSimpleTimer = poParameters.miSliderSimpleTimer;
				miSliderSpeedExtendStart = poParameters.miSliderSpeedExtendStart;
				miSliderSpeedExtendFinish = poParameters.miSliderSpeedExtendFinish;

				miSliderStepPart = poParameters.miSliderStepPart;
				mbStepOrPart = poParameters.mbStepOrPart;

				miSliderTimeHit = poParameters.miSliderTimeHit;
				mbTimeOrHit = poParameters.mbTimeOrHit;

				mbSliderRepeatLoop = poParameters.mbSliderRepeatLoop;
				mbIsRepeatLoopChecked = poParameters.mbIsRepeatLoopChecked;
				mbRepeatOrLoop = poParameters.mbRepeatOrLoop;

				msLanguage = poParameters.msLanguage;

				ActivatePreset ( );
			}
			catch ( Exception ex )
			{
				Logger.Log ( ex );
			}
		}

		public void LoadPreset ( )
		{
			try
			{
				ActivatePreset ( );
			}
			catch ( Exception ex )
			{
				Logger.Log ( ex );
			}
		}

		public void SavePreset ( string fileName )
		{
			try
			{
				FileStream fsStream = new FileStream ( fileName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite );
				BinaryFormatter bfFormatter = new BinaryFormatter ( );

				bfFormatter.Serialize ( fsStream, this );
				fsStream.Close ( );
				//ActivatePreset ( );
			}
			catch ( Exception ex )
			{
				Logger.Log ( ex );
			}
		}

		#endregion
	}
}
